package com.jh3502.helper.function.listrule;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jh3502.helper.R;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ListRuleChooseAdapter extends RecyclerView.Adapter<ListRuleChooseAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    private ArrayList<Map<String, Object>> listItems;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener = null;
    private ButtonInterface viewClick;

    public ListRuleChooseAdapter(Context context, ArrayList<Map<String, Object>> datas) {
        this.listItems = datas;
    }

    public void setButtonSetOnclick(ButtonInterface buttonInterface) {
        this.viewClick = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list_rule_choose, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        convertView.setOnLongClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position + "");
        viewHolder.itemListRuleChooseTitle.setText((String) listItems.get(position).get("name"));
//        viewHolder.item_home1_date.setText((String) listItems.get(position).get("createTime"));
////        viewHolder.item_home1_date.setText(Utils.formatDisplayTime(Long.parseLong((String) listItems.get(       ).get("id"))));
//        viewHolder.item_home1_down.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                // TODO Auto-generated method stub
//                if (viewClick != null) {
//                    //接口实例化后的而对象，调用重写后的方法
//                    viewClick.onClick(position);
//
//                }
//
//            }
//
//        });
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((String) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(@NonNull View v) {
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.onItemLongClick((String) v.getTag());
        }
        return true;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(String s);
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(String s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_list_rule_choose_title)
        TextView itemListRuleChooseTitle;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}